package lookas;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Arrays;

public class FileToDatabase {

	public static void main(String[] args) throws Exception {
		Class.forName("org.apache.derby.jdbc.ClientDriver");
		Connection con = DriverManager
				.getConnection("jdbc:derby://localhost:1527/db");
		// PreparedStatement stmt = con
		// .prepareStatement(
		// "create table imiona (imie varchar(256) not null, licznosc int not null, primary key(imie))"
		// );
		// stmt.execute();
		// PreparedStatement stmt = con
		// .prepareStatement("select count(0) from nazwiska");
		// ResultSet res = stmt.executeQuery();
		// while (res.next()) {
		// System.out.println(res.getInt(1));
		// }

		FileReader f = new FileReader("c:\\Imionafrekw.txt");
		BufferedReader r = new BufferedReader(f);
		String line;
		int x = 0;
		PreparedStatement stmt = con
				.prepareStatement("insert into imiona values (?, ?)");
		while ((line = r.readLine()) != null) {
			String[] split = line.split("=");
			Integer valueOf = Integer.valueOf(split[1]);
			stmt.setString(1, split[0]);
			stmt.setInt(2, valueOf);
			stmt.addBatch();
			x++;
			if (x > 50) {
				System.out.println(Arrays.toString(stmt.executeBatch()));
				x = 0;
			}
		}
		System.out.println(Arrays.toString(stmt.executeBatch()));
		stmt.close();
		con.close();
	}
}
